﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_PSD_TYPE : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.CommandArgument == "")
        {
            tbl_psd_type model = new tbl_psd_type();
            model.PSD_TYPE = txtPSD_TYPE.Text.Trim();
            model.IS_ACTIVE = Convert.ToBoolean(chkIS_ACTIVE.Checked);
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_psd_type bll = new BLL_tbl_psd_type();
            try
            {



                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_psd_type model = new tbl_psd_type();
            model.PSD_ID = Convert.ToInt32(btnSave.CommandArgument);
            model.PSD_TYPE = txtPSD_TYPE.Text.Trim();
            model.IS_ACTIVE = Convert.ToBoolean(chkIS_ACTIVE.Checked);
            model.MODIFIED_BY = User.Identity.Name;
            model.MDIFIED_DATE = DateTime.Now;



            BLL_tbl_psd_type bll = new BLL_tbl_psd_type();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_psd_type model = new tbl_psd_type();
        model.PSD_ID = Convert.ToInt32(id); ;

        BLL_tbl_psd_type bll = new BLL_tbl_psd_type();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_psd_type bll = new BLL_tbl_psd_type();
                List<tbl_psd_type> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_psd_type tbl in list)
                {
                    btnSave.CommandArgument = tbl.PSD_ID.ToString();
                    txtPSD_TYPE.Text = tbl.PSD_TYPE;
                    chkIS_ACTIVE.Checked =Convert.ToBoolean(tbl.IS_ACTIVE);
                }
            }
        }
    }


                     
}